package ${project.group}.${project.name}.vehicle;

import android.os.Bundle;
import android.view.View;

import com.idogfooding.backbone.ui.rv.SpaceDecoration;
import ${project.group}.${project.name}.Const;
import ${project.group}.${project.name}.R;

import com.chenenyu.router.Router;
import com.chenenyu.router.annotation.Route;
import com.idogfooding.backbone.network.ApiException;
import com.idogfooding.backbone.network.PageResult;
import com.idogfooding.backbone.ui.rv.RVFragment;
import ${project.group}.${project.name}.network.Api;
import ${project.group}.${project.name}.network.ApiCallback;
import ${project.group}.${project.name}.network.ApiHttpResult;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;



@Route({"VehicleListFragment"})
public class VehicleListFragment extends RVFragment<Vehicle, VehicleListAdapter> {

    @Override
    protected int getLayoutId() {
        return R.layout.common_list;
    }

    @Override
    protected void onConfig(Bundle savedInstanceState) {
        super.onConfig(savedInstanceState);
    }

    @Override
    protected void onSetup(View view, Bundle savedInstanceState) {
        super.onSetup(view, savedInstanceState);
        mTopButton.show();
        loadData(true, false);
    }

    @Override
    protected void createAdapter() {
        mAdapter = new VehicleListAdapter();
    }

    @Override
    protected void cfgAdapter() {
        setLoadMore(true);
        super.cfgAdapter();
        mAdapter.setOnItemClickListener((baseQuickAdapter, view, position) -> {
            Vehicle data = (Vehicle) baseQuickAdapter.getItem(position);
            Router.build("VehicleView").with("data",data)
                    .with("viewType", Const.ViewType.VIEW)
                    .go(this);
        });
    }


    @Override
    protected void loadData(boolean refresh, boolean loadMore) {
        super.loadData(refresh, loadMore);
        OkGo.<ApiHttpResult<PageResult<Vehicle>>>get(Api.VEC_LIST)
                .params("pageSize", pageSize)
                .params("pageNo", pageNumber)
                .tag(this)
                .execute(new ApiCallback<ApiHttpResult<PageResult<Vehicle>>>(this, true) {
                    @Override
                    public void onSuccessData(Response<ApiHttpResult<PageResult<Vehicle>>> response, ApiHttpResult<PageResult<Vehicle>> data) {
                        if (data.getCode() == 0) {
                            if (data.getData() != null) {
                                onLoadNext(data.getData(), refresh, loadMore);
                            } else {
                                mAdapter.setEmptyView(getEmptyView());
                            }
                        } else {
                            mAdapter.setEmptyView(getErrorView());
                            onLoadApiException(new ApiException(data.getCode(), data.getMsg()));
                        }
                        this.showLoading = false;

                    }

                    @Override
                    protected void onApiError
                            (Response<ApiHttpResult<PageResult<Vehicle>>> response,
                             ApiException exception) {
                        this.showLoading = false;
                        if (exception.getCode() == 500) {
                            super.onApiError(response, exception);
                            this.showLoading = false;
                        }
                    }
                });
    }

    @Override
    protected void cfgItemDecoration() {
        mRecyclerView.addItemDecoration(new SpaceDecoration(10));
    }


}
